﻿//动作参数相关
packageContent.actionParameter = function(actionItemId) {
	var _actionItemId;
	var _actionParamId;
	var _paramActionRelaMap;
	var _paramTypeMap;
	
	var _initData = function() {
		_actionItemId = actionItemId;
	}
	
	var _initActonParamComp = function() {
		$('#addParamValue').window( {
			title : '参数值添加',
			width : 400,
			modal : true,
			shadow : false,
			closed : true,
			resizable : false,
			draggable : true,
			minimizable : false,
			maximizable : false
		});
		$('#addParamActionRela').window( {
			title : '参数决定动作添加',
			width : 400,
			modal : true,
			shadow : false,
			closed : true,
			resizable : false,
			draggable : true,
			minimizable : false,
			maximizable : false
		});
		_initActionParam();
	}
	
	var _initActionParam = function() {
		var _actionItem = pack.getActionItemMap().get(_actionItemId);
		var _actionParamList = _actionItem.getActionItemParameterList();
		if (_actionParamList && _actionParamList.length > 0) {
			$.each(_actionParamList, function(index, actionParam) {
				_addActionParam(actionParam);
			});
		}
	}
	
	//获取前台前台生成的条目ID（p_开头的）
	var _getNewItemIdById = function(itemIdFromDB) {
		var keys = itemIdMap.keys();
		var newItemId = itemIdFromDB;
		for (var i = 0; i < keys.length; i++) {
			var itemId = itemIdMap.get(keys[i]);
			if (itemId == itemIdFromDB) {
				newItemId = keys[i];
				break;
			}
		}
		return newItemId;
	}
	
	var getActionItemByItemId = function(actionItemId) {
		
		//如果是后台查询到的条目ID，则先转成前台的ID，再从JS缓存中取
		if (actionItemId.indexOf("p_") == -1) {
			//如果是前台生成的条目ID，直接从JS缓存中取
			actionItemId = _getNewItemIdById(actionItemId);
		}
		return pack.getActionItemMap().get(actionItemId);
	}
	
	var _getParamValueDesc = function(paramValue, paramType) {
		var paramValueDesc = paramValue;
		if (paramType == "S01") {//新装已有不需要
			var valueArr = paramValue.split("|");
			var tempDesc = "";
			$.each(valueArr, function(index, value) {
				if (index == 0) {
					tempDesc = ACTION.PROD_ACTION_TYPE[value];
				} else {
					tempDesc += "|" + ACTION.PROD_ACTION_TYPE[value];
				}
			});
			paramValueDesc = tempDesc;
		} else if (paramType == "S03") {//付费方式
			var valueArr = paramValue.split("|");
			var tempDesc = "";
			$.each(valueArr, function(index, value) {
				if (index == 0) {
					tempDesc = ACTION.FEE_TYPE[value];
				} else {
					tempDesc += "|" + ACTION.FEE_TYPE[value];
				}
			});
			paramValueDesc = tempDesc;
		}
		return paramValueDesc;
	}
	
	var _usePackParam = function(paramId, packParamId) {
		var actionParamDiv = $("div[id='action_param_" +paramId+ "']");
		$(actionParamDiv).find('tr[id="choosePackParam"]').show();
		$(actionParamDiv).find("input[name='usePackParam" +paramId+ "'][value='usePack']").attr("checked", "checked");
		var _packParamMap = pack.getPackParamMap();
		var _packParams = _packParamMap.values();
		//初始化包参数选项
		var s = $(actionParamDiv).find("select[id='rela_pack_param']");
		$(s).html('');
		$.each(_packParams, function(index,item){
			var opt = $("<option>").text(item.getName()).val(item.getParameterId());
			$(s).append(opt);
			if (packParamId && packParamId == item.getParameterId()) { 
				$(opt).attr("selected", "selected");
			}
		});
		
		var _packParamId = $(actionParamDiv).find('select[id="rela_pack_param"]').val();
		if (packParamId) {
			_packParamId = packParamId;
			
		}
		var _packParam = _packParamMap.get(_packParamId);
		var _actionParamId = $(actionParamDiv).find("input[id='param_name']").attr("paramId");
		var _parameterType = $(actionParamDiv).find("select[id='param_type']").val();
		var _optionValue = $(actionParamDiv).find("select[id='param_value']").attr("val");
		var _defaultValue = $(actionParamDiv).find("select[id='param_def_value']").val();
		//参数ID
		$(actionParamDiv).find('input[id="param_name"]').attr("packParamId", _packParamId);
		$(actionParamDiv).find('input[id="param_name"]').attr("paramId", paramId);
		//参数名
		$(actionParamDiv).find('input[id="param_name"]').attr("value", _packParam.getName());
		//参数类型
		var _paramType = _packParam.getParameterType();
		$(actionParamDiv).find("select[id='param_type']").val(_paramType).attr("disabled", "disabled");
		//参数值
		var _paramValue = _packParam.getOptionValue();
		var _packParamDesc = _getParamValueDesc(_paramValue, _paramType);
		$(actionParamDiv).find("select[id='param_value']").attr("val", _paramValue);
		
		$(actionParamDiv).find("select[id='param_value']").attr("value", _packParamDesc);
		//默认值
		var _defaultValue = _packParam.getDefaultValue();
//		var maySelectNum = _packParam.g
		if(!(_paramValue)) {
			return ;
		}
		_setParamValues(_paramValue, _packParamDesc, paramId, _defaultValue);
		$(actionParamDiv).find("select[id='param_def_value']").attr("disabled", "disabled");
		
	}
	
	/**
	 * 获得默认值 checkbox相关控件
	 */
	var _getDefaultValueCompS02 = function(_paramDiv) {
		
		return $($($(_paramDiv).find('tr')[4]).find('td')[1]);
	}
	
	var _addNewParam = function(paramId) {
		var actionParamDiv = $("div[id='action_param_" +paramId+ "']");
		$(actionParamDiv).find('tr[id="choosePackParam"]').hide();
		$(actionParamDiv).find("input[name='usePackParam" +paramId+ "'][value='addNew']").attr("checked", "checked");
		$(actionParamDiv).find("input[id='param_name']").attr("paramId", paramId);
		$(actionParamDiv).find("input[id='param_name']").attr("packParamId", "");
		$(actionParamDiv).find("input[id='param_name']").attr("value", "");
		$(actionParamDiv).find("select[id='param_type']").removeAttr("disabled");
		$(actionParamDiv).find("select[id='param_value']").html("");
		$(actionParamDiv).find("select[id='param_value']").attr('value', "");
		$(actionParamDiv).find("select[id='param_value']").attr('val', "");
		$(actionParamDiv).find("select[id='param_def_value']").html("");
		$(actionParamDiv).find("select[id='param_def_value']").removeAttr("disabled");
		$(actionParamDiv).find('div[id="actionParamValues"]').hide();
		$(actionParamDiv).find('div[id="param_action_relas"]').hide();
		$(actionParamDiv).find('div[id="param_action_relas"]').html("");
		
	}
	
	var _setParamValues = function(paramValue, paramDesc, paramId, defValue,maySelectNum) {
		if (!(paramValue) || !(paramDesc)) {
			return ;
		}
		var paramDiv = $('div[id="action_param_'+ paramId +'"]');
		var valueArr = paramValue.split("|");
		var valueDescArr = paramDesc.split("|");
		var param_value = $(paramDiv).find("select[id='param_value']");
		var defaultValue = _getDefaultValueCompS02(paramDiv);
		var paramValues = $(paramDiv).find("select[id='paramValues']");
		var paramType = $(paramDiv).find("select[id='param_type']").val();
		if (paramType != PACKAGE.PARAMETERTYPE.key.S02) {
			defaultValue = defaultValue.find('select');
			$(defaultValue).html("");
		} else {
			$(defaultValue).html("");
		}
		$(param_value).html("");
		$(paramValues).html("");
		$(paramValues).parent().show();
		for (var i = 0; i < valueArr.length; i++) {
			var value = valueArr[i];
			var valueDesc = valueDescArr[i];
			//多选N业务，换成checkbox
			if (paramType == PACKAGE.PARAMETERTYPE.key.S02) {
				if (Validate.isNull(value)) { continue;}
				
				var opt = $('<input type="checkbox">').attr('name',value);
				if (defValue.indexOf(value) != -1) {
					opt.attr('checked',true);
				}
				$(defaultValue).append(value);
				$(defaultValue).append(opt);
			} else {
				var opt = $("<option>").text(valueDesc).attr("value", value);
				$(defaultValue).append(opt);
			}
				
			var opt1 = $("<option>").text(valueDesc).val(value);
			paramValues.append(opt1);
			
			var opt2 = $("<option>").text(valueDesc).val(value).attr("paramType", paramType);
			param_value.append(opt2);
			$(opt2).bind("dblclick", function() {
				var paramType = $(this).attr("paramType");
				if ((paramType == PACKAGE.PARAMETERTYPE.key.S00) || (paramType == PACKAGE.PARAMETERTYPE.key.S02)) {
					var value = $(this).text();
					var newValue = prompt("请输入要修改的值", value);
					if (!_isValueExist(newValue, paramId)) {
						$(this).attr("value", newValue);
						$(this).attr("text", newValue);
						_editActionParamRela(paramId, value, newValue);
						_resetParamValues(paramId);
					} else if (value != newValue) {
						alert("该值已存在，请从重新编辑！");
					}
				} else {
					alert("此参数类型值不可编辑，只有离散值类型才可修改！");
				}
			});
		}
		if (defValue && defValue != "") {
			defaultValue.val(defValue);
		}
		//多选N参数类型，添加【可选择数】input
		if (paramType == PACKAGE.PARAMETERTYPE.key.S02) {
			if (paramDiv.find('input[name=param_maySelectNum]').length == 0) {
				defaultValue.parents('tr').after($('<tr><td align="right" width="20%">可选择数:</td>'
						+ '<td colspan="4" width="80%"><input name="param_maySelectNum"></input></td>'
						+ '</tr>'));
			}
			paramDiv.find('input[name=param_maySelectNum]').val(maySelectNum);
			
		}
	}
	
	var _addActionParam = function(actionParam) {
		var paramId;
		var packParamId = "";
		var paramName = "";
		var parameterType = "";
		var optionValue = "";
		var optionValueDesc = "";
		var defaultValue = "";
		var paramActionRelaList;
		var maySelectNum;
		if (actionParam) {
			paramId = actionParam.getParameterId();
			packParamId = actionParam.getPackageParameterId();
			paramName = actionParam.getName();
			parameterType = actionParam.getParameterType();
			optionValue = actionParam.getOptionValue();
			optionValueDesc = _getParamValueDesc(optionValue, parameterType);
			defaultValue = actionParam.getDefaultValue();
			paramActionRelaList = actionParam.getParameterActionRelaList();
			maySelectNum = actionParam.getValueDownlimit();
		} else {
			paramId = packageContent.getParameterId();
		}
		var actionParamDiv = $('<div id="action_param_' + paramId + '" paramId="'+ paramId +'">');
		var paramTable = $('<table border="0" id="action_param_' +paramId+ '" class="param_table">'
						+ '<tr><td align="right">'
						+ '<span>*</span>使用包参数:</td>'
						+ '<td paramId="' +paramId +'"><input type="radio" name="usePackParam' +paramId+'" value="usePack"/>是'
					    + '<input type="radio" name="usePackParam' +paramId+ '" value="addNew" checked="checked"/>否(新增)</td>'
						+ '<td align="right" colspan="2"><span style="float:right"><a href="#" id="param_del" paramId="'+ paramId +'">删除条目参数</a></span></td>'
						+ '</tr><tr id="choosePackParam" style="display:none">'
						+ '<td align="right"><span class="tb_right">对应包参数:</span></td>'
						+ '<td colspan="4" ><select paramId="'+ paramId +'" id="rela_pack_param" style="width:370px;margin-right:10px"" />'
						+ '</td></tr>'
						+ '<tr><td align="right"><span>*</span>参数名称:</td>'
						+ '<td><input type="text" id="param_name" paramId="'+paramId+'" packParamId="'+packParamId
						+ '" value="'+ paramName +'" title="限制字数20"/></td>'
						+ '<td align="right">参数类型:</td>'
						+ '<td><select id="param_type" paramId="' +paramId+ '" style="width:150px;margin-right:10px" >'
						+ '<option value="S00">离散值类型（多选一）</option>'
						+ '<option value="S02">多选多离散值（N选M）</option>'
						+ '<option value="S01">产品新装已有类型，可选Y|N|D</option>'
						+ '<option value="S03">设置预后标识</option>'
						+ '<option disabled>----------------</option>'
						+ '<option value="P01">选号</option>'
						+ '<option value="P02">校验非主接入号</option>'
						+ '<option value="P03">选址</option>'
						+ '<option value="P04">用户密码</option>'
						+ '<option value="P05">帐户</option>'
						+ '</select></td></tr>');
		var paramValueDiv = $('<tr><td align="right"><span>*</span>参数值:<br/></td>'
						+ '<td colspan="3">'
						+ '<select id="param_value" multiple="multiple" size="5" readonly="readonly" style="width:370px;height:85px;margin-right:10px" val="' +optionValue+ '">'+ optionValueDesc +'</select></td>'
						+ '<td>'
						+ '<div style="margin-top:0px;margin-bottom:0px;padding-top:0px;padding-bottom:0px"><img id="param_value_add" class="imgParamAdd" title="添加" paramId="'+ paramId +'" /></div>'
						+ '<div style="margin-top:0px;margin-bottom:0px;padding-top:0px;padding-bottom:0px"><img id="param_value_del" class="imgParamDel" title="删除" paramId="'+ paramId +'" /></div>'
						+ '<div style="margin-top:0px;margin-bottom:0px;padding-top:0px;padding-bottom:0px"><img id="param_value_edit" class="imgEdit" title="编辑" paramId="'+ paramId +'"/></div>'
						+ '<div style="margin-top:0px;margin-bottom:0px;padding-top:0px;padding-bottom:0px"><img id="param_move_up" class="imgMoveUp" title="上移" paramId="'+ paramId +'"/></div>'
						+ '<div style="margin-top:0px;margin-bottom:0px;padding-top:0px;padding-bottom:0px"><img id="param_move_down" class="imgMoveDown" title="下移" paramId="'+ paramId +'"/></div>'
						+ '</td></tr>');
		var paramDefaultValueDiv = $('<tr><td align="right" width="20%">默认取值:</td>'
						+ '<td colspan="4" width="80%"><select id="param_def_value" style="width:370px;margin-right:10px"></select></td>'
						+ '</tr>');
		var paramTableEnd = $('</table>'); 
		var paramRelaSelDiv = $('<div id="actionParamValues" width="100%"><span width="20%">参数值决定条目:</span>'
    					+ '<select id="paramValues" style="width:360px;margin-right:10px"></select>'
    					+ '<a href="#"><img id="ation_param_rela_add" title="添加参数决定条目" paramId="' +paramId+ '" ' +
    					'class="imgAddParam" align="right" /></a></div>');
    	var paramRelaDiv = $('<div id="param_action_relas" paramId="' +paramId+ '" class="container"></div>');
		
    	if (paramId != 0) {
			$(actionParamDiv).append("<hr style='color:white;height:5px;'>");
		}
    	//表格内内容
		$(paramTable).append(paramValueDiv);
		$(paramTable).append(paramDefaultValueDiv);
		$(paramTable).append(paramTableEnd);
		
		$(actionParamDiv).append(paramTable);
		$(actionParamDiv).append(paramRelaSelDiv);
		$(actionParamDiv).append(paramRelaDiv);
		$("#param_content").append(actionParamDiv);
		
		//设置参数类型
		if (parameterType && parameterType != "") {
			$(paramTable).find('select[id="param_type"]').val(parameterType);
		}
		
		//table相关控件绑定事件
		_bindEventOnTableComp(paramTable, paramId);
		
		//内容参数
		if (PACKAGE.PARAMETERTYPE.contentParam.contains(parameterType)) {
			_hideSomeComp(paramTable);
		} else {
			//设置参数Table的值和事件
			_setParamValues(optionValue, optionValueDesc, paramId, defaultValue,maySelectNum);
			
			//设置参数决定动作DIV的值和事件
			$(paramRelaSelDiv).find('img[id="ation_param_rela_add"]').bind("click", function() {
				var paramId = $(this).attr("paramId");
				_addNewParamActionRela(paramId, _actionItemId);
			});
			
			//如果参数中存在参数决定动作，添加到参数决定动作DIV下
			if (paramActionRelaList && paramActionRelaList.length > 0) {
				$.each(paramActionRelaList, function(index, paramActionRela) {
					_addExistsParamRela(paramActionRela, paramRelaDiv);
				});
			}
		}
		
		var _packParamMap = pack.getPackParamMap();
    	if (packParamId && _packParamMap && _packParamMap.size() > 0) {
    		_usePackParam(paramId, packParamId);
    	}
	}
	
	
	var _hideSomeComp = function(_paramTable, paramType) {
		$(_paramTable).find('img[id="param_value_add"]').attr('style','display:none');
		$(_paramTable).find('img[id="param_value_del"]').attr('style','display:none');
		$(_paramTable).find('img[id="param_value_edit"]').attr('style','display:none');
		$(_paramTable).find('img[id="param_move_up"]').attr('style','display:none');
		$(_paramTable).find('img[id="param_move_down"]').attr('style','display:none');
	}
	
	var _showSomeComp = function(_paramTable, paramType) {
		$(_paramTable).find('img[id="param_value_add"]').attr('style','display:null');
		$(_paramTable).find('img[id="param_value_del"]').attr('style','display:null');
		$(_paramTable).find('img[id="param_value_edit"]').attr('style','display:null');
		$(_paramTable).find('img[id="param_move_up"]').attr('style','display:null');
		$(_paramTable).find('img[id="param_move_down"]').attr('style','display:null');
	}
	
	/**
	 * 为表格相关内容控件绑定事件
	 */
	var _bindEventOnTableComp = function(_paramTable, _paramId) {
		
		//参数类型事件
		$(_paramTable).find('select[id="param_type"]').bind("change", function() {
			var paramType = $(this).val();
			if (PACKAGE.PARAMETERTYPE.contentParam.contains(paramType)) {
				var paramId =  $(this).attr("paramId");
				_delAllParamValue(paramId);
				$(_paramTable).find('input[id="param_name"]').val($(this).find('option:selected').text());
				_hideSomeComp(_paramTable, paramType);
			} else {
				_showSomeComp(_paramTable, paramType);
			}
			
			//多选N  参数类型
			if (PACKAGE.PARAMETERTYPE.key.S02 == paramType) {
				$(_paramTable).find('select[id="param_def_value"]').parents('tr').after($('<tr><td align="right" width="20%">可选择数:</td>'
						+ '<td colspan="4" width="80%"><input name="param_maySelectNum"></input></td>'
						+ '</tr>'));
				$(_paramTable).find('select[id="param_def_value"]').remove();
			} else{
				if ($(_paramTable).find('select[id="param_def_value"]').length == 0) {
					_getDefaultValueCompS02(_paramTable).html("");
					_getDefaultValueCompS02(_paramTable).append('<select id="param_def_value" style="width:370px;margin-right:10px"></select>');
					_getDefaultValueCompS02(_paramTable).parents('tr').next().remove();
				}
			}
		});
		
		$(_paramTable).find('img[id="param_value_add"]').click(function() {
			var paramType = $(this).parents("table").find('select[id="param_type"]').val();
			var paramId =  $(this).attr("paramId");
			_addParamValue(paramType, paramId);
		});
		$(_paramTable).find('img[id="param_value_del"]').click(function() {
			var paramId =  $(this).attr("paramId");
			_delSelectedParamValue(paramId);
		});
		$(_paramTable).find('img[id="param_value_edit"]').click(function() {
			var paramId =  $(this).attr("paramId");
			_editSelectedParamValue(paramId);
		});
		$(_paramTable).find('img[id="param_move_up"]').click(function() {
			var paramId =  $(this).attr("paramId");
			_paramValueMoveUp(paramId);
		});
		$(_paramTable).find('img[id="param_move_down"]').click(function() {
			var paramId =  $(this).attr("paramId");
			_paramValueMoveDown(paramId);
		});
		$(_paramTable).find("select[id='param_type']").bind("change", function() {
			var paramId =  $(this).attr("paramId");
			_delAllParamValue(paramId);
		});
		$(_paramTable).find('a[id="param_del"]').click(function() {
			var paramId =  $(this).attr("paramId");
			$('div[id="action_param_' + paramId + '"]').remove();
		});
		$(_paramTable).find("input[name='usePackParam" +_paramId+ "']").bind("click", function() {
			var paramId = $(this).parent().attr("paramId");
			if(_ifUsePackParam(paramId)) {
				var _packParamMap = pack.getPackParamMap();
				if (_packParamMap && _packParamMap.size() > 0) {
					_usePackParam(paramId);
					//屏蔽新增参数等
					var actionParamDiv = $("div[id='action_param_" +paramId+ "']");
					_hideSomeComp(actionParamDiv);
				} else {
					alert("包中未设置包参数，请选择新增！");
//					$('#choosePackParam').hide();
					$(this).parent().find("input[name='usePackParam" +paramId+ "'][value='addNew']").attr("checked", "checked");
					return;
				}
			} else {
				_addNewParam(paramId);
				//屏蔽新增参数等
				var actionParamDiv = $("div[id='action_param_" +paramId+ "']");
				_showSomeComp(actionParamDiv);
			}
		});
		$(_paramTable).find("select[id='rela_pack_param']").bind("change", function() {
			var paramId = $(this).attr("paramId");
			var packParamId = $(this).val();
			_usePackParam(paramId, packParamId);
		});
	}
	var _addParamValue = function(paramType, paramId) {
		$('#addParamValue').window('open');
		$('#addParamValue').window('refresh',
				'PackagePathDispatcher?path=addActionParamValue.jsp?paramType=' + paramType + '&paramId=' + paramId);
	}
	
	var _isValueExist = function(value, paramId) {
		var flag = false;
		var actionParamDiv = $('div[id="action_param_' + paramId + '"]');
		var paramOptions = $(actionParamDiv).find("select[id='param_value']").find("option");
		$.each(paramOptions, function(index, paramOption) {
			var paramValue = $(paramOption).val();
			if (paramValue == value) {
				flag = true;
				return;
			}
		});
		return flag;
	}
	
	var _paramValueMoveUp = function(paramId) {
		var actionParamDiv = $('div[id="action_param_' + paramId + '"]');
		var paramSelectComp = $(actionParamDiv).find("select[id='param_value']");
		var first_can_move_up = $(paramSelectComp).find('option:selected:first').prev().size();
      	if(first_can_move_up){
        	$.each($(paramSelectComp).find('option:selected'), function(index, option){
				var current = $(option);
			   	var prev = $(option).prev();
			   	$(option).remove();
			   	prev.before(current);
          	});
      	}
	}
	
	var _paramValueMoveDown = function(paramId) {
		var actionParamDiv = $('div[id="action_param_' + paramId + '"]');
		var paramSelectComp = $(actionParamDiv).find("select[id='param_value']");
		var last_can_move_down = $(paramSelectComp).find('option:selected:last').next().size();
      	if(last_can_move_down){
        	$.each($(paramSelectComp).find('option:selected'), function(index, option){
        		var current = $(option);
			   	var next = $(option).next();
			   	$(option).remove();
			   	next.after(current);
        	});
      	}
	}
	var _resetParamValues = function(paramId) {
		var actionParamDiv = $('div[id="action_param_' + paramId + '"]');
		var paramValueOptions = $(actionParamDiv).find("select[id='param_value']").find("option");
		var paramValues = "";
		$.each(paramValueOptions, function(index, paramValueOption) {
			var value = $(paramValueOption).attr("value");
			if (index == 0) {
				paramValues = value;
			} else {
				paramValues += "|" + value;
			}
		});
		$(actionParamDiv).find("select[id='param_value']").attr('val', paramValues);
		var paramType = $(actionParamDiv).find("select[id='param_type']").val();
		var paramValueDesc = _getParamValueDesc(paramValues, paramType);
		var defValue;
		var maySelectNum;
		if (paramType == PACKAGE.PARAMETERTYPE.key.S02) {
			defValue = [];
			var _arr = $(actionParamDiv).find("input[type=checkbox]:checked");
			$.each(_arr,function(){
				defValue.push($(this).attr('name'));
			});
			defValue = defValue.join('|');
			maySelectNum = $(actionParamDiv).find('input[name=param_maySelectNum]').val();
		} else {
			defValue = $(actionParamDiv).find("select[id='param_def_value']").val();
		}
		_setParamValues(paramValues, paramValueDesc, paramId, defValue,maySelectNum);
	}
	var _delAllParamValue = function(paramId) {
		var actionParamDiv = $('div[id="action_param_' + paramId + '"]');
		$(actionParamDiv).find("input[id='param_name']").attr("packParamId", "");
		//删除参数值
		$(actionParamDiv).find("select[id='param_value']").html("");
		$(actionParamDiv).find("select[id='param_value']").attr('val', "");
		//删除默认参数
		$(actionParamDiv).find("select[id='param_def_value']").html("");
		//删除参数值决定条目
		$(actionParamDiv).find('div[id="actionParamValues"]').hide();
		$(actionParamDiv).find('div[id="param_action_relas"]').hide();
		$(actionParamDiv).find('div[id="param_action_relas"]').html("");
	}
	
	var _delSelectedParamValue = function(paramId) {
		var actionParamDiv = $('div[id="action_param_' + paramId + '"]');
		$(actionParamDiv).find("input[id='param_name']").attr("packParamId", "");
		var paramValueComp = $(actionParamDiv).find("select[id='param_value']");
		//被选择的参数值
		var selectedParamOptions = $(paramValueComp).find("option:selected");
		if (selectedParamOptions.size() > 0) {
			$.each(selectedParamOptions, function(index, selectedParamOption) {
				var value = $(selectedParamOption).attr("value");
				//删除被选中的参数值
				$(selectedParamOption).remove();
				//删除参数值决定条目
				_removeActionParamRela(paramId, value);
			});
			
			_resetParamValues(paramId);
		
		} else {
			alert("请选择要删除的参数值！");
		}
	}
	var _editSelectedParamValue = function(paramId) {
		var actionParamDiv = $('div[id="action_param_' + paramId + '"]');
		var paramType = $(actionParamDiv).find("select[id='param_type']").val();
		$(actionParamDiv).find("input[id='param_name']").attr("packParamId", "");
		
		var paramValueComp = $(actionParamDiv).find("select[id='param_value']");
		//被选择的参数值
		var selectedParamOptions = $(paramValueComp).find("option:selected");
		if ((paramType == "S00") || (paramType == PACKAGE.PARAMETERTYPE.key.S02)) {
			if (selectedParamOptions.size() == 1) {
				var selectedParamOption = selectedParamOptions[0]
				var value = $(selectedParamOption).attr("value");
				var newValue = prompt("请输入要修改的值", value);
				if (!_isValueExist(newValue, paramId)) {
					$(selectedParamOption).attr("value", newValue);
					$(selectedParamOption).attr("text", newValue);
					_editActionParamRela(paramId, value, newValue);
					_resetParamValues(paramId);
				} else if (value != newValue) {
					alert("该值已存在，请从重新编辑！");
				}
			} else if (selectedParamOptions.size() > 1) {
				alert("每次只能编辑一个参数值！");
			} else {
				alert("请选择要编辑的参数值！");
			}
		} else {
			alert("此参数类型值不可编辑，只有离散值类型才可修改！");
		}
	}
	var _removeActionParamRela = function(actionParamId, value) {
		var actionRelas = $('div[id="action_param_'+ actionParamId +'"]').find('div[id="param_action_relas"]').find("div");
		if (actionRelas && actionRelas.size() > 0) {
			$.each(actionRelas, function(index, item) {
				var paramValue = $(item).find("span[id='paramValue']").attr("val");
				if (value == paramValue) {
					$(item).remove();
				}
			});
		}
	}
	var _editActionParamRela = function(actionParamId, value, newValue) {
		var actionRelas = $('div[id="action_param_'+ actionParamId +'"]').find('div[id="param_action_relas"]').find("div");
		if (actionRelas && actionRelas.size() > 0) {
			$.each(actionRelas, function(index, item) {
				var paramValue = $(item).find("span[id='paramValue']").attr("val");
				if (value == paramValue) {
					$(item).find("span[id='paramValue']").attr("val", newValue).html("【" +newValue+ "】");
				}
			});
		}
	}
	
	var _ifUsePackParam = function(paramId) {
		var actionParamDiv = $("div[id='action_param_" +paramId+ "']");
		var val = $(actionParamDiv).find("input:radio[name='usePackParam" +paramId+ "']:checked").val();
		if (val == 'usePack') {
			return true;
		} else if (val == 'addNew') {
			return false;
		}
	}
	
	var _addNewParamActionRela = function(paramId, actionItemId) {
		
		var paramValue = $("div[paramid="+paramId+"] #paramValues").val();
		if(Validate.isNull(paramValue)) {
			alert("请首先添加参数值！");
			return ;
		}
		
		$('#addParamActionRela').window('open');
		$('#addParamActionRela').window('refresh',
				'PackagePathDispatcher?path=addParamActionRela.jsp?paramId='+ paramId 
				+ '&actionItemId=' + actionItemId);
	}
	
	var _addExistsParamRela = function(paramActionRela, paramRelaDiv) {
		if (!paramActionRela) {
			return;
		}
		var actionItemId = paramActionRela.getActionItemId();
		var itemNumber;
		//p_条目ID
		if (itemIdMap.getValueFromKey(actionItemId) != "null") {
			itemNumber = packageContent.getItemNumberByItemId(itemIdMap.getValueFromKey(actionItemId));
		} else {
		//入库条目ID
			itemNumber = packageContent.getItemNumberByItemId(actionItemId);
		}
		var actionItemDesc = "(" + itemNumber + ") ";
		var actionItem = getActionItemByItemId(actionItemId);
		if (actionItem) {
			actionItemId = actionItem.getActionItemId();
			if (actionItem.getDescription()) {
				actionItemDesc += actionItem.getDescription();
			}
		} else {
			return;
		}
		
		var relaType = paramActionRela.getRelaType();
		var paramId = $(paramRelaDiv).attr("paramId");
		var paramType = $('table[id="action_param_'+ paramId +'"]').find('select[id="param_type"]').val();
		var paramValue = paramActionRela.getValue();
		var paramValueDesc = _getParamValueDesc(paramValue, paramType);
		var grpId = paramActionRela.getGrpId();
		var actionRela = '<div grpId='+grpId+'><span id="paramValue" val="' + paramValue + '">【'+paramValueDesc+'】：</span>'
			+ '<span id="actionItem" val="' + actionItemId + '">【' + actionItemDesc + '】</span>'
			+ '<span id="relaType" val="' + relaType + '">【' +ACTION.RELA_TYPE[relaType]+ '】</span>'
    		+ '<a href="#"><img id="ation_param_rela_del" class="imgDelParam" title="删除参数决定条目" align="right"/></a>'
    		+ '</div>';
		$(paramRelaDiv).append($(actionRela));
		$(paramRelaDiv).find('img[id="ation_param_rela_del"]').click(function(){
    		$(this).parent().parent().remove();
    	});
	}
	
	var _validate = function() {
		var flag = true;
		var actionParamDivs = $("div[id^='action_param_']");
		var paramTypes = [];
		$.each(actionParamDivs, function(index, actionParamDiv) {
			var _name = $(actionParamDiv).find("input[id='param_name']").val();
			var _optionValue = $(actionParamDiv).find("select[id='param_value']").attr("val");
			var _parameterType = $(actionParamDiv).find("select[id='param_type']").val();
			if (paramTypes.contains(_parameterType)) {
				alert("相同类型的参数不能添加多条！");
				flag = false;
				return;
			} else {
				paramTypes.push(_parameterType);
			}
			if (!_name || _name == "") {
				alert("【参数名】不能为空！");
				flag = false;
				return;
			} else if (!PACKAGE.PARAMETERTYPE.contentParam.contains(_parameterType) && (!_optionValue || _optionValue == "")) {
				alert("【参数值】不能为空！");
				flag = false;
				return;
			}
			if (_name.length > 20) {
				alert("【参数名】不能大于20字符！");
				flag = false;
				return;
			}
			
			if (_parameterType == PACKAGE.PARAMETERTYPE.key.S02) {
				find('input[name]:checked');
				if(Validate.isNull($(actionParamDiv).find('input[name=param_maySelectNum]').val())) {
					alert("请填写【可选择数】！");
					flag = false;
					return;
				}
				if($(actionParamDiv).find('input[type=checkbox]').length < $(actionParamDiv).find('input[name=param_maySelectNum]').val()) {
					alert("【可选择数】不能大于【参数个数】，请修改！");
					flag = false;
					return;
				}
				if($(actionParamDiv).find('input[type=checkbox]:checked').length > $(actionParamDiv).find('input[name=param_maySelectNum]').val()) {
					alert("【可选择数】不能小于【默认选择数】，请修改！");
					flag = false;
					return;
				}
				
			}
		});
		return flag;
	}
	
	var _saveActionParameter = function() {
		//保存动作参数
		var actionParamDivs = $("div[id^='action_param_']");
		//先取条目
		var _actionItemMap = pack.getActionItemMap();
		var _actionItem = _actionItemMap.get(_actionItemId);
		//再根据条目取参数
		var _actionParamList = [];
		$.each(actionParamDivs, function(index, actionParamDiv) {
			var _optionValue = "";
			var _valueOptions = $(actionParamDiv).find("select[id='param_value']").find("option");
			$.each(_valueOptions, function(index, valueOption) {
				if (index == 0) {
					_optionValue = $(valueOption).val();
				} else {
					_optionValue += "|" + $(valueOption).val();
				}
			});
			var _name = $(actionParamDiv).find("input[id='param_name']").val();
			var _actionParamId = $(actionParamDiv).find("input[id='param_name']").attr("paramId");
			var _parameterType = $(actionParamDiv).find("select[id='param_type']").val();
			var _defaultValueComp;
			var _defaultValue;
			var _maySelectNum;
			if ((_parameterType == PACKAGE.PARAMETERTYPE.key.S00)) {//多选1
				_defaultValue = $(actionParamDiv).find("select[id='param_def_value']").val();
			} else if(_parameterType == PACKAGE.PARAMETERTYPE.key.S02) {//多选N
				_defaultValue = [];
				_defaultValueComp = _getDefaultValueCompS02(actionParamDiv);
				_defaultValueComp = _getDefaultValueCompS02(actionParamDiv).find('input[name]:checked');
				$.each(_defaultValueComp,function(){
					_defaultValue.push($(this).attr('name'));
				});
				_defaultValue = _defaultValue.join('|');
				
				_maySelectNum = $(actionParamDiv).find('input[name=param_maySelectNum]').val();
			}
			
			var _packParamId = $(actionParamDiv).find("input[id='param_name']").attr("packParamId");
			
			var _actionParam = new ActionParameter();
			_actionParam.setParameterId(_actionParamId);
			_actionParam.setName(_name);
			//如果是查询出来的，保存时需要保存原来的条目ID
			if (itemIdMap.get(_actionItemId)) {
				_actionParam.setActionItemId(itemIdMap.get(_actionItemId));
			} else {
				_actionParam.setActionItemId(_actionItemId);
			}
			_actionParam.setParameterType(_parameterType);
			_actionParam.setDefaultValue(_defaultValue);
			_actionParam.setOptionValue(_optionValue);
			if (_parameterType == PACKAGE.PARAMETERTYPE.key.S02) {
				_actionParam.setValueDownlimit(_maySelectNum);
				_actionParam.setValueUplimit(_maySelectNum);
			}
			if (_packParamId && _packParamId != "undefined") {
				_actionParam.setPackageParameterId(_packParamId);
			}
			//保存参数决定条目
			_saveParamActionRela(_actionParam);
			//保存参数到条目
			_actionParamList.push(_actionParam);
		});
		_actionItem.setActionItemParameterList(_actionParamList);
		//保存条目到全局JS
		_actionItemMap.put(_actionItemId, _actionItem);
		pack.setActionItemMap(_actionItemMap);
		
		if (_actionParamList.length > 0) {
			//整体逻辑 ：第二列 第3个
			var itemActionTd = $("#" + _actionItemId).next().find("td")[2];
			var span = $(itemActionTd).find("span");
			var _firstParamName = _actionParamList[0].getName();
			var _allParamName = _firstParamName;
			if (_actionParamList.length > 1) {
				$.each(_actionParamList, function(index, actionParam) {
					if (index != 0) {
						_allParamName += "，" + actionParam.getName();
					}
				});
				span.attr("title", _allParamName);
				var tempName = _firstParamName + "...更多";
				if(tempName.length > 10) {
					_firstParamName = _firstParamName.substring(0,10)+"...更多";
				} else {
					_firstParamName = tempName;
				}
				span.html(_firstParamName);
			} else {
				span.attr("title", _firstParamName);
				if(_firstParamName.length > 10) {
					_firstParamName = _firstParamName.substring(0,12)+"...";
				}
				span.html(_firstParamName);
			}
		}
	}
	
	var _saveParamActionRela = function(_actionParam) {
		var paramActionRelaList = [];
		var _actionParamId = _actionParam.getParameterId();
		var actionRelas = $('div[id="action_param_'+ _actionParamId +'"]').find('div[id="param_action_relas"]').find("div");
		if (actionRelas.size() > 0) {
			$.each(actionRelas, function(index, item) {
    			var paramValue = $(item).find("span[id='paramValue']").attr("val");
    			var actionItem = $(item).find("span[id='actionItem']").attr("val");
    			var relaType = $(item).find("span[id='relaType']").attr("val");
    			var grpId = $(item).attr("grpId");
    			var paramActionRela = new ParameterActionRela();
				paramActionRela.setParameterId(_actionParamId);
				paramActionRela.setValue(paramValue);
				//如果是查询出来的，保存时需要保存原来的条目ID
				if (itemIdMap.get(actionItem)) {
					actionItem = itemIdMap.get(actionItem);
				}
				paramActionRela.setActionItemId(actionItem);
				paramActionRela.setRelaType(relaType);
				paramActionRela.setGrpId(grpId);
				paramActionRelaList.push(paramActionRela);
			});
		}
		_actionParam.setParameterActionRelaList(paramActionRelaList);
	}
	
	//保存
	var _doSave = function() {
		if (!_validate()) {
			return;
		}
		_saveActionParameter();
		_doClose();
	}
	
	//关闭
	var _doClose = function() {
		//保存动作参数
		var actionParamDivs = $("div[id^='action_param_']");
		if (actionParamDivs.size() == 0) {
			var _actionItem = pack.getActionItemMap().get(_actionItemId);
			var _actionParamList = _actionItem.getActionItemParameterList();
			if (!_actionParamList || _actionParamList.length == 0) {
				//整体逻辑 ：第二列 第3个
				var itemActionTd = $("#" + _actionItemId).next().find("td")[2];
				$(itemActionTd).find("span").html("设置");
			}
		}
		$("#openActionParameterPage").window('close');
	}
	
	var _initButton = function() {
		$("#action_param_save").bind("click", function() {
			_doSave();
		});
		$("#action_param_cancel").bind("click", function() {
			_doClose();
		});
		
		$('#param_add_btn').click(function() {
			_addActionParam();
			
		});

	}
	
	//初始条目参数
	var _init = function() {
		_initData();
		_initActonParamComp();
		_initButton();
	}

	_init();
	
}
